package com.linkage.jz.bankruptcy.activity.Project;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.linkage.jz.bankruptcy.BankruptcyApp;
import com.linkage.jz.bankruptcy.Consts;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.activity.Vote.ProVoteNewVoteActivity;
import com.linkage.jz.bankruptcy.bean.MyAddr;
import com.linkage.jz.bankruptcy.net.BaseSubscriber;
import com.linkage.jz.bankruptcy.net.Network;
import com.linkage.jz.bankruptcy.net.ToastUtil;
import com.linkage.jz.bankruptcy.utils.SPUtils;
import com.linkage.jz.bankruptcy.widget.CommonPromptDialog;
import com.linkage.jz.bankruptcy.widget.ShapeLoadingDialog;
import com.linkage.jz.bankruptcy.widget.ZQTimeNoticeDialog;
import com.linkage.seapeak.activity.message.MultiMessageHistoryActivity;
import com.linkage.seapeak.activity.message.MultiSelectActivity;
import com.linkage.seapeak.base.BaseWhiteActivity;

import org.json.JSONObject;

import java.io.IOException;
import java.util.List;
import java.util.Random;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * A login screen that offers login via email/password.
 */
public class ProZQChangePhoneActivity extends BaseWhiteActivity implements OnClickListener {
    private static final String TAG = ProZQChangePhoneActivity.class.getSimpleName();

    private ShapeLoadingDialog shapeLoadingDialog;

    TextView tv_et_org_num, btn_save;
    EditText et_org;

   private static String mProId,mCreditorId;

    public static void start(Context context,String proId,String creditorId) {
        Intent intent = new Intent(context, ProZQChangePhoneActivity.class);
        context.startActivity(intent);
        mProId = proId;
        mCreditorId = creditorId;
    }
    private CommonPromptDialog mPromptDialog;
    @Override
    public void onBackPressed() {
        //super.onBackPressed();
        mPromptDialog = new CommonPromptDialog(this, CommonPromptDialog.TYPE_TIPS);
        mPromptDialog.setContent("放弃编辑？");
        mPromptDialog.setPositiveClickListener("是", view -> ProZQChangePhoneActivity.super.onBackPressed());
        mPromptDialog.setNegativeClickListener("否", view -> mPromptDialog.dismiss());
        mPromptDialog.show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pro_zq_change_phone, "填写手机号码");

        initview();
        shapeLoadingDialog = new ShapeLoadingDialog(this);
    }

    //初始化视图
    private void initview() {
        //登录层、下拉层、其它登录方式层
        btn_save = findViewById(R.id.btn_save);
        tv_et_org_num = findViewById(R.id.tv_et_org_num);
        et_org = findViewById(R.id.et_org);

        btn_save.setOnClickListener(this);
        showSave();

        addMenu(new MenuAction(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String nickname = et_org.getText().toString();
                if ("".equalsIgnoreCase(nickname)) {
                    showToast("手机号不能为空");
                    return;
                }

                zqTimeNoticeDialog = new ZQTimeNoticeDialog(ProZQChangePhoneActivity.this, new View.OnClickListener() {
                    @Override
                    public void onClick( View view ) {
                        String  sendStatus = "";
                        if (view.getId() == R.id.btn_chancel_notice) {//暂存
                            sendStatus = "1";
                            zqTimeNoticeDialog.dismiss();
                        } else if (view.getId() == R.id.ll_close) {
                            if (zqTimeNoticeDialog != null && zqTimeNoticeDialog.isShowing())
                                zqTimeNoticeDialog.dismiss();

                            return;
                        } else {//生成


                            if(!zqTimeNoticeDialog.send_msg.isChecked() && !zqTimeNoticeDialog.send_phone.isChecked())
                            {
                                Toast.makeText(ProZQChangePhoneActivity.this,
                                        "请选择发送方式",Toast.LENGTH_SHORT).show();
                                return;
                            }
                            zqTimeNoticeDialog.dismiss();

                            String msg = "";
                            if(zqTimeNoticeDialog.send_msg.isChecked()&& zqTimeNoticeDialog.send_phone.isChecked())
                            {
                                msg = "0,1";
                            }else if(zqTimeNoticeDialog.send_msg.isChecked())
                            {
                                msg = "0";
                            }else if(zqTimeNoticeDialog.send_phone.isChecked())
                            {
                                msg = "1";
                            }

                            SaveData(nickname,msg);
                        }

                        zqTimeNoticeDialog.dismiss();

                    }
                }, 1);
                zqTimeNoticeDialog.show();
            }
        }, "完成", 0));

        et_org.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                        textView.setText(s.toString());

//                tv_et_org_num.setText(s.length() + "/30");
//                showSave();
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        showSave();

    }

    private void showSave()
    {
        String str1 =  et_org.getText().toString().trim();


        if(!str1.equalsIgnoreCase("") )
        {

            btn_save.setClickable(true);
            btn_save.setBackgroundResource((R.drawable.login_corner_btn));
        }else{
            btn_save.setClickable(false);
            btn_save.setBackgroundResource((R.drawable.login_corner_nosel_btn));
        }


    }



    private ZQTimeNoticeDialog zqTimeNoticeDialog;
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_save:
                final String nickname = et_org.getText().toString();
                if ("".equalsIgnoreCase(nickname)) {
                    showToast("手机号不能为空");
                    return;
                }

                zqTimeNoticeDialog = new ZQTimeNoticeDialog(ProZQChangePhoneActivity.this, new View.OnClickListener() {
                    @Override
                    public void onClick( View view ) {
                        String  sendStatus = "";
                        if (view.getId() == R.id.btn_chancel_notice) {//暂存
                            sendStatus = "1";
                            zqTimeNoticeDialog.dismiss();
                        } else if (view.getId() == R.id.ll_close) {
                            if (zqTimeNoticeDialog != null && zqTimeNoticeDialog.isShowing())
                                zqTimeNoticeDialog.dismiss();

                            return;
                        } else {//生成


                            if(!zqTimeNoticeDialog.send_msg.isChecked() && !zqTimeNoticeDialog.send_phone.isChecked())
                            {
                                Toast.makeText(ProZQChangePhoneActivity.this,
                                        "请选择发送方式",Toast.LENGTH_SHORT).show();
                                return;
                            }
                            zqTimeNoticeDialog.dismiss();

                            String msg = "";
                            if(zqTimeNoticeDialog.send_msg.isChecked()&& zqTimeNoticeDialog.send_phone.isChecked())
                            {
                                msg = "0,1";
                            }else if(zqTimeNoticeDialog.send_msg.isChecked())
                            {
                                msg = "0";
                            }else if(zqTimeNoticeDialog.send_phone.isChecked())
                            {
                                msg = "1";
                            }

                            SaveData(nickname,msg);
                        }

                        zqTimeNoticeDialog.dismiss();

                    }
                }, 1);
                zqTimeNoticeDialog.show();
                break;
        }
    }

    private void SaveData(String phone,String msg) {

        String userToken = SPUtils.getInstance(Consts.SPNAME).getString("userToken", "");

        Network
                .getAPIService()
                .addCreditorMobile(userToken,mProId,mCreditorId,phone,msg)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            super.onNext(json);
                            return;
                        }

                        ToastUtil.showShort("提交成功");
                        finish();
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }


}

